/**
 * Created by user on 2/13/19.
 */

import { QuestionActionType } from '@act/questionAction'
import { singleSort, singleClone, Type, Data } from '@utils/Global'

const question_state = {
  show: false,
  questionList: [],
  answerList: [],
  realList: []
}
const QuestionState = (state = question_state, action) => {
  let _state = singleClone(state)
  switch (action.type) {
    case QuestionActionType.get_transfer_question_success_msg:
      let clone = singleClone(action.payload.msg)
      _state.questionList.push(clone.data.question)
      _state.realList.push(Object.assign({}, clone.data.question, {
        teachers: []
      }))
      return _state
    case QuestionActionType.toggle_show:
      return Object.assign({}, state, {
        show: action.payload.show
      })
    case QuestionActionType.init_question_list:
      let d = intQuestion(action.payload)
      return Object.assign({}, state, d)
    case QuestionActionType.get_answer_msg:
      return state
    case QuestionActionType.get_answer_up_msg:
      return state
    case QuestionActionType.get_question_msg:
      return state
    case QuestionActionType.get_question_up_msg:
      return state
    case QuestionActionType.get_question_or_answer_msg:
      const { msgType, msg, isRemoteSelf, isSelf } = action.payload
      let cloneMsg = singleClone(msg), filter = null
      if (msgType === 'question') {
        _state.questionList.push(cloneMsg.data.question)
        _state.realList.push(Object.assign({}, cloneMsg.data.question, {
          teachers: []
        }))
      }
      if (msgType === 'answer') {
        _state.answerList.push(cloneMsg.data.answer)
        if (cloneMsg.data.answer.data.type === 1) {
          cloneMsg.data.answer.voiceStatus = {
            playing: false,
            loading_voice: false,
            load_error: false,
            record_time: Math.ceil(cloneMsg.data.answer.data.record_time / 1000)
          }
        }
        let cq = _state.realList.find(r => r.id === cloneMsg.data.answer.question_id)
        if (!cq) {
          return _state
        }
        let ct = cq.teachers.find(r => r.teacher.id === cloneMsg.data.answer.user.id)
        if (!ct) {
          cq.teachers.push({
            teacher: cloneMsg.data.answer.user,
            answers: [cloneMsg.data.answer]
          })
          return _state
        }
        ct.answers.push(cloneMsg.data.answer)
      }
      if (msgType === 'question_up') {
        filter = _state.realList.find(r => r.id === cloneMsg.data.question.id)
        if (filter) {
          filter.like = cloneMsg.data.question.like
          filter.up_num = cloneMsg.data.question.up_num
          filter.users = cloneMsg.data.question.users
          _state.realList = singleSort(_state.realList, 'up_num')
        }
      }
      if (msgType === 'answer_up') {
        try {
          let _cq = _state.realList.find(r => r.id === cloneMsg.data.answer.question_id)
          let _ct = _cq && _cq.teachers.find(r => r.teacher.id === cloneMsg.data.answer.user.id)
          filter = _ct && _ct.answers.find(r => r.id === cloneMsg.data.answer.id)
        } catch (e) {}
        if (filter) {
          filter.like = cloneMsg.data.answer.like
          filter.up_num = cloneMsg.data.answer.up_num
          filter.users = cloneMsg.data.answer.users
        }
      }
      return _state
    case QuestionActionType.do_self_like_item:
      if (action.payload.msgType === 'question') {
        let selfFilter = _state.realList.find(r => r.id === action.payload.id)
        if (selfFilter) {
          selfFilter.up_num += 1
          selfFilter.like = true
          selfFilter.users.push({
            id: Data.user.id,
            name: Data.user.nickname,
            headimgurl: Data.user.headimgurl
          })
        }
      }
      if (action.payload.msgType === 'answer') {
        let selfAnswer = _state.answerList.find(r => r.id === action.payload.id)
        if (selfAnswer) {
          let selfcQ = _state.realList.find(r => r.id === selfAnswer.question_id)
          if (selfcQ) {
            let selfcT = selfcQ.teachers.find(r => r.teacher.id === selfAnswer.user.id)
            if (selfcT) {
              let selfcA = selfcT.answers.find(r => r.id === selfAnswer.id)
              if (selfcA) {
                selfcA.up_num += 1
                selfcA.like = true
                selfcA.users.push({
                  id: Data.user.id,
                  name: Data.user.nickname,
                  headimgurl: Data.user.headimgurl
                })
              }
            }
          }
        }
      }
      return _state
    case QuestionActionType.update_voice_status:
      let upQid = parseInt(action.payload.aId.split('#')[0])
      let upTid = parseInt(action.payload.aId.split('#')[1])
      let upAid = parseInt(action.payload.aId.split('#')[2])
      let upItem = null
      try {
        upItem = _state.realList.find(q => q.id === upQid).teachers
          .find(t => t.teacher.id === upTid).answers
          .find(a => a.id === upAid)
      } catch (e) {}
      if (upItem && upItem.voiceStatus) {
        let cloneStatus = singleClone(upItem.voiceStatus)
        upItem.voiceStatus = Object.assign({}, cloneStatus, action.payload.status)
      }
      return _state
    case QuestionActionType.stop_all_voice:
      _state.realList.map(q => {
        q.teachers.map(t => {
          t.answers.map(a => {
            if (a.voiceStatus && a.data.type === 1) {
              a.voiceStatus.playing = false
              a.voiceStatus.loading = false
            }
          })
        })
      })
      return _state
    default:
      return state
  }
}
function intQuestion({ questions, answers }) {
  let questionList = questions.map(q => {
    let _answers = answers.filter(a => a.question_id === q.id)
    let _answerTeachers = _answers.map(r => r.user)
    let _answerTeacherIds = _answerTeachers.map(r => r.id)
    _answerTeacherIds = Array.from(new Set(_answerTeacherIds))
    let qAnswers = _answerTeacherIds.map(r => {
      return {
        teacher: _answerTeachers.find(_ => r === _.id),
        answers: _answers.filter(_ => _.user.id === r).map(_ => {
          let rest = {}
          if (_.data.type === 1) {
            rest.voiceStatus = {
              playing: false,
              loading_voice: false,
              load_error: false,
              record_time: Math.ceil(_.data.record_time / 1000)
            }
          }
          return Object.assign({}, _, {
            aId: `${q.id}#${r}#${_.id}`,
            isStaring: false,
            ...rest
          })
        })
      }
    })
    return Object.assign({}, q, {
      teachers: qAnswers,
      users: q.users.slice(0, 5)
    })
  })
  return {
    questionList: singleClone(questions),
    answerList: singleClone(answers),
    realList: singleSort(questionList, 'up_num')
  }
}
QuestionState._default_state = question_state
export default QuestionState
